package com.yuelao.thread.demo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池测试
 * 线程池的种类：固定线程池、单实例线程池、自动调节线程池、循环执行线程池
 * Created by fengliye on 2017/10/7.
 */
public class ThreadPoolTest {
    private static ExecutorService threadPool = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        try {
            // 有返回值提交
            String s0 = (String)threadPool.submit(new CallTest()).get();
            System.out.println(s0);
            String s1 = (String)threadPool.submit(() ->{
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("s1");
            }, "ok").get();
            System.out.println(s1);

            threadPool.execute(() ->{
                System.out.println("t3");
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    static class CallTest implements Callable{
        @Override
        public Object call() throws Exception {
            System.out.println("calling");
            return "success";
        }
    }
}
